/**
 * author: zhou
 * date: 2017.10.18
 * description: 菜单控制中心
 * functions: {
 *      save: 保存菜单
 *      findOne: 查询菜单
 *      find: 查询菜单列表
 *      findAll: 查询所有菜单
 *      del: 删除菜单
 *      update: 更新菜单
 * }
 * 
 */

const   mongoPromise = require('../../../core/mongo/mongoPromise'),
        Menu    = require('../../../core/mongo/models/system/menuModel');

const menuController = {
    /**
     * 保存菜单
     * 如果为二级菜单或三级菜单,需要检查upLevel字段是否为空
     * @param {*} ctx 
     */
    async save(ctx) {
        const { name, level, upLevel, url, icon, type } = ctx.request.body.data;
        if (!name) {
            ctx.body = { success: false, message: '菜单名称不能为空', state: 10004 };
            return;
        }
        if (!url) {
            ctx.body = { success: false, message: '菜URL不能为空', state: 10004 };
            return;
        }
        if (!icon) {
            ctx.body = { success: false, message: '菜单图标不能为空', state: 10004 };
            return;
        }
        if (!type) {
            ctx.body = { success: false, message: '菜单类型不能为空', state: 10004 };
            return;
        }
        if (level !== '1' && !upLevel) {
            ctx.body = { success: false, message: '上级菜单不能为空', state: 10004 };
            return;
        }
        const menu = new Menu({
            name,
            level,
            upLevel: upLevel || '',
            url,
            icon,
            type
        });
        await Menu.create(menu).then(() => {
            ctx.body = { success: true, data: [], state: 200 }
        },
        (error) => {
            ctx.body = { success: false, message: '保存失败', state: 10003 }
        });
    },
    /**
     * 查询菜单
     * @param {*} ctx 
     */
    async findOne(ctx) {
        const { id } = ctx.request.body.data;
        if (!id) {
            ctx.body = { success: false, message: '待删除菜单id不能为空', state: 10004 };
            return;
        }
        const query = Menu.findById(id);
        const result = await mongoPromise(ctx, query);
        if (result) {
            ctx.body = { success: true, data: result, state: 200 };
        }
    },
    /**
     * 查询菜单列表
     * @param {*} ctx 
     */
    async find(ctx) {
        const { level } = ctx.request.body.data;
        if (!level) {
            ctx.body = { success: false, message: '上级菜单不能为空', state: 10004 };
            return;
        }
        const query = Menu.find({ level });
        const result = await mongoPromise(ctx, query);
        if (result) {
            ctx.body = { success: true, data: result, state: 200 };
        }
    },
    /**
     * 查询全部菜单列表
     * @param {*} ctx 
     */
    async findAll(ctx) {
        const { page, limit } = ctx.request.body.data;
        if (!page || !limit) {
            ctx.body = { success: false, message: '抱歉,参数有误', state: 10004 };
            return;
        }
        const query = Menu.find().skip((page - 1) * limit).limit(limit);
        const result = await mongoPromise(ctx, query);
        if (result) {
            const countQuery = Menu.count();
            const countResult = await mongoPromise(ctx, countQuery);
            if (countResult !== undefined) {
                ctx.body = { success: true, data: { data: result, count: countResult }, state: 200 };
            }
        }
    },
    /**
     * 删除菜单
     * 如果待删除菜单存在子菜单,不能删除
     * @param {*} ctx 
     */
    async del(ctx) {
        const { id } = ctx.request.body.data;
        if (!id) {
            ctx.body = { success: false, message: '待删除菜单id不能为空', state: 10004 };
            return;
        }
        const query = Menu.find({ upLevel: id });
        const result = await mongoPromise(ctx, query);
        if (result.length > 0) {
            ctx.body = { success: false, message: '待删除菜单存在下级菜单,不能删除', state: 10004 };
        } else {
            const delQuery = Menu.remove({ _id: id });
            const delResult = await mongoPromise(ctx, delQuery);
            if (delResult) {
                ctx.body = { success: true, data: [], state: 200 };
            }
        }
    },
    /**
     * 更新菜单
     * @param {*} ctx 
     */
    async update(ctx) {
        const { _id, name, level, upLevel, url, icon, type } = ctx.request.body.data;
        if (!_id) {
            ctx.body = { success: false, message: '菜单id不能为空', state: 10004 };
            return;
        }
        if (!name) {
            ctx.body = { success: false, message: '菜单名称不能为空', state: 10004 };
            return;
        }
        if (!url) {
            ctx.body = { success: false, message: '菜URL不能为空', state: 10004 };
            return;
        }
        if (!icon) {
            ctx.body = { success: false, message: '菜单图标不能为空', state: 10004 };
            return;
        }
        if (!type) {
            ctx.body = { success: false, message: '菜单类型不能为空', state: 10004 };
            return;
        }
        if (level !== '1' && !upLevel) {
            ctx.body = { success: false, message: '上级菜单不能为空', state: 10004 };
            return;
        }
        const menu = {
            name,
            level,
            upLevel: upLevel || '',
            url,
            icon,
            type
        };
        const query = Menu.update({ _id }, menu);
        const result = await mongoPromise(ctx, query);
        if (result) {
            ctx.body = { success: true, data: [], state: 200 };
        }
    }
};

module.exports = menuController;